/**
 * Imported packages
 */
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;


/**
 *
 * @authors BENSON WACHIRA, DOROTHY SARPONG, STANLEY ANKU
 * MICHAEL ESHUN & MAKAFUI AMEZAH
 */

/**
 * 
 * A controller for the modify order class
 */
public class ControllerModifyOrder {
     
    /**
     * Objects and variable declarations
     */
    Connection connection=null;
    String deleteStudent;
    String query;
    Statement stmt=null;
    public ModifyOrder modifyOrder;
    private ActionListener actionListener;
    
    
    /**
     * Creates a new ControllerModifyOrder form
     */
    public ControllerModifyOrder(){
        modifyOrder = new ModifyOrder();
        modifyOrder.setVisible(true);
    }
   
    /**
     * Method displays all data in  database for modification
     */
    public void showAllDetails(){
        String key="";
        key = modifyOrder.getPassportID().getText();
        try{
            Class.forName("com.mysql.jdbc.Driver");
            connection =(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/jetwingdb?zeroDateTimeBehavior=convertToNull","root","");
            query="SELECT * FROM Orders  WHERE passPortNo LIKE'%"+key+"%'";
            stmt=connection.prepareStatement(query);
            stmt.execute("USE jetwingdb;");

            ResultSet rs= stmt.executeQuery(query);
        
            while(rs.next()){
 
                modifyOrder.getNameField().setText(rs.getString("name"));
                modifyOrder.getPassportField().setText(rs.getString("passPortNo"));
                modifyOrder.getEmailField().setText(rs.getString("email"));
                modifyOrder.getPhoneField().setText(rs.getString("phoneNO"));
                modifyOrder.getFlightField().setText(rs.getString("flightNo"));
                modifyOrder.getSeatField().setText(rs.getString("seatNo"));
                modifyOrder.getFromField().setText(rs.getString("fFrom"));
                modifyOrder.getDestinationField().setText(rs.getString("destination"));
                modifyOrder.getDepartureField().setText(rs.getString("depatureTime")); 
                modifyOrder.getArrivalTimeField().setText(rs.getString("arrivalTime"));
        }
  
        stmt.close();
        connection.close();
        } 
        
        catch(Exception e){
            JOptionPane.showMessageDialog(null,e);
        }
    
    }
    
    /**
     * Method writes back changes to database
     */
    public void insertChanges(){
        String key="";
        key = modifyOrder.getPassportID().getText();
    try{
        Class.forName("com.mysql.jdbc.Driver");
        //jdbc:mysql://localhost:3306/jetwingdb?zeroDateTimeBehavior=convertToNull [root on Default schema]
        connection =(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/jetwingdb?zeroDateTimeBehavior=convertToNull","root","");
        query="UPDATE Orders SET name=?, passPortNo=?, email=?, phoneNo=?, flightNo=?, seatNo=?, fFrom=?, destination=?, depatureTime=?, arrivalTime=?"
                + "where passPortNo like '%"+ key +"%';";
//        querry = "update flights set fName =?,ffrom=?,destination=?,Price=?,fCapacity=?,depatureTime=?,arrivalTime=?"
//                    + "where fName like '%" + ky + "%'";
        PreparedStatement st=connection.prepareStatement(query);
        st.execute("USE jetwingdb;");

   st.setString(1, modifyOrder.getNameField().getText());
   st.setString(2, modifyOrder.getPassportField().getText());
   st.setString(3, modifyOrder.getEmailField().getText());
   st.setString(4, modifyOrder.getPhoneField().getText());
   st.setString(5, modifyOrder.getFlightField().getText());
   st.setString(6, modifyOrder.getSeatField().getText());
   st.setString(7, modifyOrder.getFromField().getText());
   st.setString(8, modifyOrder.getDestinationField().getText());
   st.setString(9, modifyOrder.getDepartureField().getText());
   st.setString(10, modifyOrder.getArrivalTimeField().getText());
   st.execute();
   JOptionPane.showMessageDialog(null, "Order modified succesfully");
   stmt.close();
   connection.close();
  
    }  
    catch(Exception e){
        JOptionPane.showMessageDialog(null,e);
    }
    
    }
    
    /**
     * Method to get source of button clicked using the action listener
     */
    public void control(){        
        actionListener = new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {  
                if(actionEvent.getSource()==modifyOrder.getSaveButton()){
                    insertChanges();
                }
                if(actionEvent.getSource()==modifyOrder.getCloseButton()){
                    modifyOrder.setVisible(false);
                }
                if(actionEvent.getSource()==modifyOrder.getSearchButton()){
                  showAllDetails();  
                }
            }
    };               
        modifyOrder.getSaveButton().addActionListener(actionListener);
        modifyOrder.getCloseButton().addActionListener(actionListener);
        modifyOrder.getSearchButton().addActionListener(actionListener);
    }
     
    
}

